//
//  WYCSelectionPlanController.m
//  信用卡管理
//
//  Created by 中企互联 on 2017/11/15.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCSelectionPlanController.h"
#import "WYCSelectionModel.h"
#import "WYCSelectionCell.h"



@interface WYCSelectionPlanController ()<UITableViewDelegate,UITableViewDataSource>



/** 数组*/
@property(nonatomic,strong)NSMutableArray * addArray;


/** 初始化列表*/
@property(nonatomic,strong)UITableView * tableView;
@end

@implementation WYCSelectionPlanController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBarTintColor:WYCRGBColor(229, 50, 67)];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.title = @"选择消费计划";
    
    self.addArray = [[NSMutableArray alloc] init];
    
    [self setupRequster];
    
    [self setupTableView];
    
    //确定按钮
    [self setupReturn];
    
    
    
    
    
}

#pragma make --------确定按钮
- (void)setupReturn{
    UIButton * returnButton = [[UIButton alloc] init];
    returnButton.frame = CGRectMake( 40 * px, ScreenHeight - 200 * px - 40 * px - 64, ScreenWidth - 80 * px, 150 * px);
    returnButton.backgroundColor = WYCMain;
    [returnButton setTitle:@"确定" forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(returnButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [returnButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:returnButton];
    
}

- (void)setupTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavHeight, ScreenWidth, ScreenHeight / 1.5) style:UITableViewStylePlain];
    //    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
}
- (void)setupRequster {
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"bankId"] = self.bankId;
    params[@"dailyAmount"] = self.dailyAmount;
    params[@"balance"] = self.balance;
    [HttpTool POST:Consumeplanlist params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                WYCSelectionModel * selectionModel = [WYCSelectionModel mj_objectWithKeyValues:dict];
                [self.addArray addObject:selectionModel];
            }
            if (self.addArray.count != 0) {
                WYCSelectionModel * model = self.addArray[0];
                model.isSelect = YES;
            }else{
                
            }
            
        }else{
            [self showToast:msgStr];
            
        }
        [self.tableView reloadData];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.addArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    WYCSelectionCell * cell = [WYCSelectionCell cellWithTableView:tableView];
    cell.selectionModel = self.addArray[indexPath.row];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (WYCSelectionModel * model in self.addArray) {
        model.isSelect = NO;
    }
    WYCSelectionModel * model = self.addArray[indexPath.row];
    model.isSelect = YES;
    [self.tableView reloadData];
    
    
}

- (void)returnButtonClick{
    WYCLogFunc;
    for (WYCSelectionModel * model in self.addArray) {
        
        if (model.isSelect == YES) {
            NSString * str = [NSString stringWithFormat:@"日消费%@笔",model.maxNumber];
            [self.Delegate setDelegatemaxNumber:str setID:model.planId];
            WYCLog(@"%@",str);
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }
    
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    
    
    CALayer *bottomLineLayer = [[CALayer alloc] init];
    bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    bottomLineLayer.backgroundColor = WYCmainColor.CGColor;
    [NavView.layer addSublayer:bottomLineLayer];
    
    
    
    
    
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"选择消费计划";
    [NavView addSubview:NavRigsterLabel];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end

